//
//  PostSuggestionViewController.m
//  MingShi
//
//  Created by MJiao on 15/7/2.
//  Copyright (c) 2015年 MJiao. All rights reserved.
//

#import "PostSuggestionViewController.h"
#import "KVNProgress.h"
#import "UserManager.h"
#import "AFHTTPRequestOperationManager.h"


@interface PostSuggestionViewController ()

@end

@implementation PostSuggestionViewController
- (IBAction)btnSumbitAction:(id)sender {

    NSString *content = [self.textView text];
    NSString *title = [self.tfTitle text];

    if (title == nil || title.length == 0) {
        [KVNProgress showErrorWithStatus:@"请填写标题"];
        [self.tfTitle becomeFirstResponder];
        return;
    }
    if (content == nil || content.length == 0) {
        [KVNProgress showErrorWithStatus:@"请填写建议内容"];
        [self.textView becomeFirstResponder];
        return;
    }
    // Adds a status below the circle
    [KVNProgress showWithStatus:@"提交中..."];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

    NSString *urlString = @"http://121.41.91.36:8888/api/require/setreq";

    NSInteger  userId = [[[UserManager shareUserManager] curUser] userId];
    NSString * userIdStr = [NSString stringWithFormat:@"%ld",userId];
    
    NSDictionary *parameters = @{
                @"userId": userIdStr,
                @"title":title,
                @"Contents":content
    };
    [manager POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

        // 结束登陆提示
        [KVNProgress dismiss];

        NSString *code = [responseObject objectForKey:@"code"];
        //登录失败
        if (code == nil || ![code isEqualToString:@"00"]) {
            [KVNProgress showErrorWithStatus:[responseObject objectForKey:@"msg"]];
            return;
        }

        [KVNProgress showSuccessWithStatus:[responseObject objectForKey:@"msg"] completion:^{
            // Things you want to do after the HUD is gone.
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
    }    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        // 结束登陆提示
        [KVNProgress dismiss];
        [KVNProgress showErrorWithStatus:@"网络异常,请稍后再试"];
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
     
    self.viewTextViewBg.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
    self.viewTextViewBg.layer.borderWidth = 1.0;
    
    self.viewTextViewBg.layer.cornerRadius = 5;
    self.viewTextViewBg.layer.masksToBounds = YES;
    
    self.textView.placeholder = @"请输入建议内容";
    [self.textView setText:@""];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
